/**
 * 
 */
package org.mspring.mlog.web.module.admin.spider;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.api.spider.entity.PublishRule;
import org.mspring.mlog.api.spider.entity.SpiderPost;
import org.mspring.mlog.api.spider.entity.SpiderRule;
import org.mspring.mlog.api.spider.service.PublishRuleService;
import org.mspring.mlog.api.spider.service.SpiderPostService;
import org.mspring.mlog.api.spider.service.SpiderRuleService;
import org.mspring.mlog.entity.NewsCatalog;
import org.mspring.mlog.service.NewsCatalogService;
import org.mspring.mlog.web.query.SpiderPostQueryCriterion;
import org.mspring.mlog.web.security.SecurityUtils;
import org.mspring.platform.dao.support.Page;
import org.mspring.platform.dao.support.Sort;
import org.mspring.platform.utils.SessionUtils;
import org.mspring.platform.web.ResponseEntity;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author Gao Youbo
 * @since 2013-5-3
 * @description
 * @TODO
 */
@Widget
@RequestMapping("/admin/spider/post")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Admin_SpiderPostWidget extends Spider_AbstractWidget {
    @Autowired
    private SpiderPostService spiderPostService;
    @Autowired
    private SpiderRuleService spiderRuleService;
    @Autowired
    private NewsCatalogService newsCatalogService;
    @Autowired
    private PublishRuleService publishRuleService;

    @RequestMapping("/list")
    public String list(@ModelAttribute Page<SpiderPost> spiderPostPage, @ModelAttribute SpiderPost spiderPost, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (spiderPostPage == null) {
            spiderPostPage = new Page<SpiderPost>();
        }
        if (spiderPostPage.getSort() == null) {
            spiderPostPage.setSort(new Sort("id", Sort.DESC));
        }
        spiderPostPage = spiderPostService.listPage(new SpiderPostQueryCriterion(queryParams), spiderPostPage);

        List<SpiderRule> spiderRules = spiderRuleService.findAllEnabledRules();
        List<PublishRule> publishRules = publishRuleService.getAll();

        model.addAttribute("spiderPostPage", spiderPostPage);
        model.addAttribute("spiderRules", spiderRules);
        model.addAttribute("publishRules", publishRules);
        model.addAllAttributes(queryParams);

        // 分类
        List<NewsCatalog> catalogs = newsCatalogService.getAll();
        model.addAttribute("catalogs", catalogs);
        return "/admin/spider/post/listSpiderPost";
    }

    /**
     * 查看文章
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/view")
    public String view(@RequestParam(required = false) Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            id = SessionUtils.getAttrAsLong(request, "Admin_SpiderPostWidget_view_id");
        }
        if (id == null) {
            return prompt(model, "请选择要查看的文章");
        }
        SpiderPost spiderPost = spiderPostService.get(id);
        if (spiderPost == null) {
            return prompt(model, "未找到要查看的文章");
        }
        SessionUtils.setAttr(request, "Admin_SpiderPostWidget_view_id", spiderPost.getId());
        model.addAttribute("spiderPost", spiderPost);
        return "/admin/spider/post/viewSpiderPost";
    }

    /**
     * 查看文章
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam(required = false) Long id, @ModelAttribute SpiderPost spiderPost, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            id = SessionUtils.getAttrAsLong(request, "Admin_SpiderPostWidget_edit_id");
        }
        if (id == null) {
            return prompt(model, "请选择要修改的文章");
        }
        spiderPost = spiderPostService.get(id);
        if (spiderPost == null) {
            return prompt(model, "未找到要修改的文章");
        }
        SessionUtils.setAttr(request, "Admin_SpiderPostWidget_edit_id", spiderPost.getId());
        model.addAttribute("spiderPost", spiderPost);
        return "/admin/spider/post/editSpiderPost";
    }

    /**
     * 查看文章
     * 
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/edit/save")
    public String edit_save(@ModelAttribute SpiderPost spiderPost, HttpServletRequest request, HttpServletResponse response, Model model) {
        spiderPostService.update(spiderPost);
        return "redirect:/admin/spider/post/edit?id=" + spiderPost.getId();
    }

    /**
     * 发布文章
     * 
     * @param id
     *            要发布的采集文章编号
     * @param catalog
     *            发不到的分类
     * @param spiderPostPage
     * @param spiderPost
     * @param queryParams
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/publish")
    @ResponseBody
    public ResponseEntity publish(@RequestParam(required = false) Long id, @RequestParam(required = false) Long catalog, HttpServletRequest request, HttpServletResponse response, Model model) {
        ResponseEntity rsp = new ResponseEntity();
        if (id == null) {
            rsp.setSuccess(false);
            rsp.setMessage("无法判定要发表文章");
            return rsp;
        }
        if (catalog == null) {
            rsp.setSuccess(false);
            rsp.setMessage("请选择要发布分类");
            return rsp;
        }
        try {
            Long userId = SecurityUtils.getCurrentUser(request).getId();
            SpiderPost post = spiderPostService.get(id);
            spiderPostService.publishPost(userId, catalog, post);
            rsp.setSuccess(true);
            return rsp;
        } catch (Exception e) {
            // TODO: handle exception
            rsp.setSuccess(false);
            rsp.setMessage(e.getMessage());
            return rsp;
        }
    }

    /**
     * 删除文章
     * 
     * @param id
     * @param spiderPostPage
     * @param spiderPost
     * @param queryParams
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(required = false) Long[] ids, @ModelAttribute Page<SpiderPost> spiderPostPage, @ModelAttribute SpiderPost spiderPost, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (ids == null || ids.length == 0) {
            return prompt(model, "请选择要操作的对象");
        }
        spiderPostService.remove(ids);
        return list(spiderPostPage, spiderPost, queryParams, request, response, model);
    }

    /**
     * 变更发布状态
     * 
     * @param id
     * @param posted
     * @param spiderPostPage
     * @param spiderPost
     * @param queryParams
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/changePosted")
    public String changePosted(@RequestParam(required = false) Long[] ids, @RequestParam(required = false) Boolean posted, @ModelAttribute Page<SpiderPost> spiderPostPage, @ModelAttribute SpiderPost spiderPost, @RequestParam Map queryParams, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (ids == null) {
            return prompt(model, "请选择要操作的对象");
        }
        spiderPostService.changePosted(posted, ids);
        return list(spiderPostPage, spiderPost, queryParams, request, response, model);
    }
}
